package com.taoist_awei.blog.api;

import com.alibaba.druid.sql.visitor.functions.Bin;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.taoist_awei.blog.core.ClassifyEntity;
import com.taoist_awei.blog.core.ClassifyNode;
import com.taoist_awei.blog.core.ClassifyRepository;
import com.taoist_awei.blog.core.HttpResult;
import com.taoist_awei.blog.exception.InvalidRequestException;
import com.taoist_awei.blog.exception.ResourceNotFoundException;
import com.taoist_awei.blog.infrastructure.repository.MyBatisClassifyRepository;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import javax.validation.constraints.NotEmpty;
import java.util.List;
import java.util.Optional;


@RestController
@RequestMapping("classify")
public class ClassifyApi {
    private ClassifyRepository repository;

    @Autowired
    public ClassifyApi(MyBatisClassifyRepository repository) {
        this.repository = repository;
    }

    @GetMapping("/all")
    public ResponseEntity<?> getClassifyTree() {
        Optional<List<ClassifyNode>> optional = this.repository.getClassifyTree();
        if (optional.isPresent()) {
            List<ClassifyNode> classifyNodes = optional.get();
            HttpResult result = new HttpResult.HttpResultBuilder()
                    .ok(classifyNodes);
            return ResponseEntity.ok(result);
        } else {
            throw new  ResourceNotFoundException();
        }
    }

    @PostMapping("add")
    public ResponseEntity<?> addClassify(@RequestBody AddParam param,
                                         BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new InvalidRequestException(bindingResult);
        }
        if (repository.getClassifyById(param.getFatherId()).isPresent()) {
            ClassifyEntity entity = new ClassifyEntity(param.getClassifyName(), param.getFatherId());
            repository.addClassify(entity);
            HttpResult result = new HttpResult.HttpResultBuilder()
                    .statusCode(200)
                    .message("创建分类成功")
                    .build();
            return ResponseEntity.ok(result);
        } else {
            HttpResult result = new HttpResult.HttpResultBuilder()
                    .message("fatherId未找到")
                    .statusCode(404)
                    .build();
            throw new InvalidRequestException(result);
        }
    }

    @PostMapping(value = "/delete")
    public ResponseEntity<?> deleteClassify(@RequestBody DeleteParam param, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            throw new InvalidRequestException(bindingResult);
        }
        repository.removeClassifyById(param.getClassifyId());
        HttpResult result = new HttpResult.HttpResultBuilder()
                .message("删除成功")
                .statusCode(200)
                .build();
        return ResponseEntity.ok(result);
    }
}

@Data
class AddParam {
    @JsonProperty("classifyName")
    @NotEmpty(message = "分类名字不能为空")
    private String classifyName;
    @JsonProperty("fatherId")
    @NotEmpty(message = "fatherId不能为空")
    private String fatherId;
}

@Data
class DeleteParam {
    @JsonProperty("classifyId")
    @NotEmpty(message = "classifyId不能为空")
    private String classifyId;
}
